﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary.Optimization
{
    /// <summary>
    /// This class defines an extremum of a mathematical polynomial.
    /// </summary>
    [Serializable]
    public class RealExtremum : IRealExtremum
    {
        /// <summary>
        /// The x value of the extremum.
        /// </summary>
        private double x;

        /// <summary>
        /// The y value of the extremum.
        /// </summary>
        private double y;

        /// <summary>
        /// The type of the extremum.
        /// </summary>
        private ExtremumType type;

        /// <summary>
        /// Initializes a new instance of the <see cref="RealExtremum"/> class.
        /// </summary>
        /// <param name="x">The x value of the extremum.</param>
        /// <param name="y">The y value of the extremum.</param>
        public RealExtremum(double x, double y)
        {
            this.x = x;
            this.y = y;
            this.type = ExtremumType.Undefined;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="RealExtremum"/> class.
        /// </summary>
        /// <param name="x">The x value of the extremum.</param>
        /// <param name="y">The y value of the extremum.</param>
        /// <param name="type">The type of the extremum.</param>
        public RealExtremum(double x, double y, ExtremumType type)
        {
            this.x = x;
            this.y = y;
            this.type = type;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="RealExtremum"/> class.
        /// </summary>
        /// <param name="extremum">The extremum to clone.</param>
        public RealExtremum(RealExtremum extremum)
        {
            if (extremum == (RealExtremum) null)
            {
                throw new ArgumentNullException("extremum");
            }

            this.x = extremum.X;
            this.y = extremum.Y;
            this.type = extremum.Type;
        }

        /// <summary>
        /// Gets or sets the x value of the extremum.
        /// </summary>
        /// <value>The x value of the extremum.</value>
        public double X
        {
            get { return x; }
            set { x = value; }
        }

        /// <summary>
        /// Gets or sets the y value of the extremum.
        /// </summary>
        /// <value>The y value of the extremum.</value>
        public double Y
        {
            get { return y; }
            set { y = value; }
        }

        /// <summary>
        /// Gets or sets the type of the extremum.
        /// </summary>
        /// <value>The type of the extremum.</value>
        public ExtremumType Type
        {
            get { return type; }
            set { type = value; }
        }

        /// <summary>
        /// Compares the current instance to another one.
        /// </summary>
        /// <param name="compare">The instance compare.</param>
        /// <returns>True if the extremum values are even otherwise, false.</returns>
        public bool CompareTo(RealExtremum compare)
        {
            return (this == compare);
        }

        /// <summary>
        /// Implements the operator ==.
        /// </summary>
        /// <param name="a">The first RealExtremum instance.</param>
        /// <param name="b">The second RealExtremum instance.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator ==(RealExtremum a, RealExtremum b)
        {
            if (System.Object.ReferenceEquals(a, b))
            {
                return true;
            }

            if (((object) a == null) || ((object) b == null))
            {
                return false;
            }

            return ((a.X == b.X) && (a.Y == b.Y) && (a.Type == b.Type));
        }

        /// <summary>
        /// Implements the operator !=.
        /// </summary>
        /// <param name="a">The first RealExtremum instance.</param>
        /// <param name="b">The second RealExtremum instance.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator !=(RealExtremum a, RealExtremum b)
        {
            return !(a == b);
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">The <paramref name="obj"/> parameter is null.</exception>
        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        /// <summary>
        /// Serves as a hash function for a particular type.
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override string ToString()
        {
            return this.x.ToString() + "; " + this.y.ToString() + " | " + this.type.ToString();
        }
    }
}